﻿using System;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace FunctionGrapher
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            chart.Series.Clear();
            chart.Series.Add(new Series());
            chart.Series[0].ChartType = SeriesChartType.Line;
            UpdateChart();
        }

        private void UpdateChart()
        {
            chart.Series[0] = new Series {ChartType = SeriesChartType.Line};
            chart.ChartAreas[0].AxisX.Interval = (double)xValues.Value;
            chart.ChartAreas[0].AxisY.Interval = (double)xValues.Value;
            for (var i = -50; i < 50; i++)
            {
                chart.Series[0].Points.AddXY(i, CalculateY(i));
            }
        }

        private void UpdateChart2()
        {
            chart.Series[1] = new Series {ChartType = SeriesChartType.Line};
            for (var i = -50; i < 50; i++)
            {
                chart.Series[1].Points.AddXY(i, CalculateY2(i));
            }
        }

        private double CalculateY(double x)
        {
            //Ax^4 + Bx^3 + Cx^2 + Dx + E
            return Math.Pow(x, 4) * (double)quarticCoefficient.Value + Math.Pow(x, 3) * 
                (double)cubicCoefficient.Value + Math.Pow(x, 2) * (double)quadraticCoefficient.Value * x 
                + (double)linearCoefficient.Value * x + (double)constant.Value;
        }


        private double CalculateY2(double x)
        {
            //Ax^4 + Bx^3 + Cx^2 + Dx + E
            return Math.Pow(x, 4) * (double)quarticCoefficient2.Value + Math.Pow(x, 3) * 
                (double)cubicCoefficient2.Value + Math.Pow(x, 2) * (double)quadraticCoefficient2.Value * x 
                + (double)linearCoefficient2.Value *x + (double)constant2.Value;
        }

        #region "Coefficients"
        private void CubicCoefficient_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart();
        }

        private void quadraticCoefficient_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart();
        }

        private void linearCoefficient_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart();
        }

        private void constant_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart();
        }
      
        private void xValues_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart();
        }

        private void quarticCoefficient_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart();
        }

        private void quarticCoefficient2_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart2();
        }

        private void cubicCoefficient2_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart2();
        }

        private void quadraticCoefficient2_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart2();
        }

        private void linearCoefficient2_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart2();
        }

        private void constant2_ValueChanged(object sender, EventArgs e)
        {
            UpdateChart2();
        }

        #endregion
    }
}
